<?php
namespace Dashboard\Controller;

use Think\Controller;
use Think\Session\Driver\Memcache;

class GoodsController extends BaseController{
    //分类
    public function index(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        //搜索
        $sou     = $_POST['sou'];
        $content = $_POST['keywords'];
        if($sou != null){ //标题
            $currpage  = 1;
            $totalpage = 1;//总页数
            if($sou == 1){
                $list      = sels("goods_type","title like '%{$content}%'");
            }
            if($sou == 2){
                $list      = sels("goods_type","jianjie like '%{$content}%'");
            }
            if($sou == 3){
                $list      = sels("goods_type","content like '%{$content}%'");
            }
        }else{
            $arr       = currpage("goods_type","","$pagesize","$currpage");
            $totalpage = $arr['totalpage'];
            $list      = $arr['list'];
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function form(){
        $id      = I('id');
        if($id != null){
            $lists       = sel("goods_type","",array("id"=>$id));
            $this->assign("list",$lists);
        }
        $this->display();
    }
    //添加/修改
    public function add(){
        $id             = $_POST['id'];
        $data['name']  = I('name');
        if($id == null){
            $data['create_at'] = time();

            $result = ins("goods_type",$data);
            if($result){
                $logs = logs("添加了项目分类：{$data['name']}");
                $this->success("添加成功",U('form'));
            }
        }else{
            $data['up_at']     = time();
            $result = up_one("goods_type",array("id"=>$id),$data);
            if($result){
                $logs = logs("修改了项目分类：{$data['name']}");
                $this->success("修改成功",U('index'));}else{$this->success("没有信息变更，修改失败");}
        }
    }
}